<template>
	<view class="container">
		<view class="null"></view>
		<view class="search-bar">
			<uni-icons type="search" size="20"></uni-icons>
			<input type="text" placeholder="搜索门店名称" v-model="searchText" />
		</view>
		<scroll-view class="store-list" scroll-y @scrolltolower="loadMore">
			<view class="store-item" v-for="(store, index) in filteredPrinters" :key="index"
				@click="viewDetails(store,2)">
				<view class="store-info">
					<view class="store-header">
						<uni-icons type="shop-filled" size="20" color="#999999"></uni-icons>
						<view class="store-name">{{ store.store_name }}</view>
					</view>
					<view class="store-address">{{ store.address }}{{ store.address_info }}</view>
					<view class="store-related">联系电话：{{store.mobile}}</view>
				</view>
				<view class="line"></view>
				<view class="store-btns">
					<!-- <radio-group @change="handleSetDefault">
						<label>
							<radio :value="store.id" :checked="store.isDefault" style="transform:scale(0.8)"></radio>
							<text style="font-weight: 400;font-size: 24rpx;color: #666666;">设为默认</text>
						</label>
					</radio-group> -->
					<view class="button">查看详情</view>
				</view>
			</view>
		</scroll-view>
		<button type="primary" class="add-btn" @click="addStore(1)">
			<uni-icons type="plusempty" size="18" color="#fff"></uni-icons>
			添加店铺
		</button>
		<view class="empty" v-if="isEmpty">
			<image src="/static/empty-icon.png" mode="widthFix"></image>
			<text>暂无数据，如有问题，请联系客服</text>
		</view>
	</view>
</template>

<script>
	import {
		storesList
	} from "@/api/userApi/user.js"
	export default {
		data() {
			return {
				storeData: [],
				isEmpty: false,
				totalCount: 0,
				searchText: '',
				params: {
					page: 1,
					page_size: 500,
				},
			};
		},
		methods: {
			// handleSetDefault(e) {
			// 	const storeId = e.detail.value;
			// 	this.storeData.forEach(store => {
			// 		if (store.id === storeId) {
			// 			store.isDefault = true;
			// 		} else {
			// 			store.isDefault = false;
			// 		}
			// 	});
			// },
			loadMore() {
				if (this.params.page * this.params.page_size >= this.totalCount) {
					return;
				}
				this.params.page++;
				this.getStoresList();
			},
			viewDetails(val, type) {
				uni.navigateTo({
					url: `/pagesUser/editStore/editStore?val=${encodeURIComponent(JSON.stringify(val))}&type=${type}`
				})
			},
			getStoresList() {
				storesList(this.params).then(res => {
					if (res.code == 200) {
						if (res.data.list.length == 0) {
							this.isEmpty = true
						} else {
							this.isEmpty = false
						}
						this.totalCount = res.data.total
						if (this.params.page === 1) {
							this.storeData = res.data.list
						} else {
							this.storeData = this.storeData.concat(res.data.list);
						}
					}
				})
			},
			addStore(type) {
				uni.navigateTo({
					url: `/pagesUser/editStore/editStore?type=${type}`
				})
			}
		},
		computed: {
			filteredPrinters() {
				if (this.searchText) {
					return this.storeData.filter(printer => printer.store_name.includes(this.searchText));
				}
				return this.storeData;
			}
		},
		onShow() {
			this.getStoresList()
		}
	};
</script>

<style lang="scss">
	.container {
		background-color: #f5f5f5;
		height: 100vh;

		.null {
			width: 100%;
			height: 20rpx;
		}

		.search-bar {
			display: flex;
			align-items: center;
			background-color: #ffffff;
			border-radius: 30rpx;
			padding: 10rpx 20rpx;
			margin-bottom: 20rpx;
			position: static;
			top: 0;

			uni-icons {
				margin-right: 10rpx;
			}

			input {
				flex: 1;
				border: none;
				background-color: transparent;
				font-size: 28rpx;
			}
		}

		.store-list {
			overflow-y: auto;

			.store-item {
				background-color: #fff;
				border-radius: 10rpx;
				padding: 20rpx;
				margin-bottom: 28rpx;
				box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);

				.store-info {
					display: flex;
					flex-direction: column;

					.store-header {
						display: flex;
						align-items: center;
						margin-bottom: 15rpx;

						uni-icons {
							margin-right: 10rpx;
						}

						.store-name {
							font-weight: 500;
							font-size: 29rpx;
							color: #333333;
						}
					}



					.store-address {
						font-weight: 400;
						font-size: 24rpx;
						color: #666666;
						margin-bottom: 15rpx;
					}

					.store-related {
						font-weight: 400;
						font-size: 29rpx;
						color: #666666;
					}
				}

				.line {
					width: 100%;
					height: 1rpx;
					background-color: #EFEFEF;
					margin: 28rpx 0;
				}

				.store-btns {
					display: flex;
					justify-content: flex-end;
					align-items: center;

					radio-group {
						margin-right: 20rpx;

						radio {
							margin-right: 5rpx;
						}
					}

					.button {
						width: 157rpx;
						height: 42rpx;
						border: 2rpx solid #999999;
						padding: 10rpx 20rpx;
						border-radius: 30rpx;
						font-weight: 400;
						font-size: 25rpx;
						color: #333333;
						text-align: center;
						line-height: 42rpx;
					}
				}

			}


		}



		.add-btn {
			position: fixed;
			bottom: 20rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 600rpx;
			height: 80rpx;
			border-radius: 40rpx;
			font-size: 28rpx;
			color: #fff;
			background-color: #3385FF;
			display: flex;
			align-items: center;
			justify-content: center;

			uni-icons {
				margin-right: 10rpx;
			}
		}

		.empty {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);

			image {
				width: 200rpx;
				margin-bottom: 20rpx;
			}

			text {
				font-size: 24rpx;
			}
		}
	}
</style>